package com.ghck.server.controller.shop;

import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ghck.server.GhckAplication;
import com.ghck.server.model.Users;
import com.ghck.server.model.response.ResultList;
import com.ghck.server.model.response.ResultObject;
import com.ghck.server.model.response.UserRes;
import com.ghck.server.security.AjkUserDetails;
import com.ghck.server.service.UsersService;
import com.ghck.server.utils.AjkUtil;
import com.ghck.shop.model.BaseInfo;
import com.ghck.shop.model.MakerRank;
import com.ghck.shop.service.StatisticsService;

@RestController
@RequestMapping(value="/statistics",method=RequestMethod.GET)
public class StatisticsController {
	
	@Autowired
	private UsersService usersService;

	@Autowired
	private StatisticsService statisticsService;
	
	
	@RequestMapping(value="/makerRankListMonth/{type}",method=RequestMethod.GET)
	public ResultList<MakerRank> makerRankListMonth(@PathVariable("type") Integer type){
		ResultList<MakerRank> result = new ResultList<MakerRank>();
		List<MakerRank> list = statisticsService.makerRankList(type);
		convert(list);
		result.setItemList(list);
		return result;
	}

	private void convert(List<MakerRank> list) {
		if(!CollectionUtils.isEmpty(list)){
			List<Integer> ids = new ArrayList<Integer>();
			for(MakerRank makerRank:list){
				ids.add(makerRank.getMakerUserId());
			}
			List<Users> users = usersService.findUsersByIds(ids);
			Map<Integer, Users> map = new HashMap<Integer, Users>();
			for(Users user:users){
				Users temp = map.get(user.getId());
				if(temp == null){
					map.put(user.getId(), user);
				}
			}
			for(MakerRank makerRank:list){
				if(map.containsKey(makerRank.getMakerUserId())){
					makerRank.setName(map.get(makerRank.getMakerUserId()).getName());
				}
				if(makerRank.getMakerUserId() == 0){
					makerRank.setName("默认代理人");
				}
			}
		}
	}
	
	@RequestMapping(value="/makerRankListAll/{type}",method=RequestMethod.GET)
	public ResultList<MakerRank> makerRankListAll(@PathVariable("type") Integer type){
		ResultList<MakerRank> result = new ResultList<MakerRank>();
		List<MakerRank> list = statisticsService.makerRankList(type);
		result.setItemList(list);
		return result;
	}
	
	@PreAuthorize(GhckAplication.shop_maker)
	@RequestMapping(value="/baseInfo",method=RequestMethod.GET)
	public ResultObject<BaseInfo> baseInfo(Principal principal){
		AjkUserDetails userDetails = AjkUtil.getUserDetails(principal);
		ResultObject<BaseInfo> result = new ResultObject<BaseInfo>();
		UserRes u = usersService.findUsersById(userDetails.getUserId());
		result.setDomain(statisticsService.getBaseInfo(userDetails.getUserId(), u.getRole()));
		return result;
	}
}
